from django.shortcuts import render_to_response
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django import newforms as forms
from django.http import HttpResponseRedirect
from nexo.menu import *

from django.utils.translation import ugettext as _

def getProfile (user):
	"""
		Create the user profile. The return value of this functions must be passed to all the templates.
	"""

#	messageAdmin = channelAdmin = admin = False
#	if checkAdmin (user):
#		messageAdmin = channelAdmin = admin = True
#	elif checkChannelAdmin (user):
#		channelAdmin = True
#	elif checkMessageAdmin (user):
#		messageAdmin = True

	profile = {}
#	profile['admin'] = admin
#	profile['channelAdmin'] = channelAdmin
#	profile['messageAdmin'] = messageAdmin
	m = []

	appendMenu (m, _('Start'), '/')
	calendarMenu = appendMenu (m, _('Calendar'), '#')
	appendSubmenu (m, calendarMenu, _('Week view'), '#')
	appendSubmenu (m, calendarMenu, _('Month view'), '#')
	appendSubmenu (m, calendarMenu, _('Year view'), '#')
	appendSubmenu (m, calendarMenu, _('Multiple year view'), '#')

	wikiMenu = appendMenu(m, _('Wiki'), '#')
	appendSubmenu (m, wikiMenu, _('Wiki\'s index'), '#')
	appendSubmenu (m, wikiMenu, _('New page'), '#')
	appendSubmenu (m, wikiMenu, _('Page\'s list'), '#')
	appendSubmenu (m, wikiMenu, _('Namespace\'s list'), '#')

#	if messageAdmin or channelAdmin:
#		sendMenu = appendMenu (m, _('Send'), '#')
#		appendSubmenu (m, sendMenu, _('Personal message'), '/manager/mensajepersonalizado')
#		appendSubmenu (m, sendMenu, _('Programmed message'), '/manager/avisoprogramado')

#		receptionMenu = appendMenu (m, _('Reception'), '#')
#		appendSubmenu (m, receptionMenu, _('Requests per channel'), '/manager/reques_per_channel')
#		appendSubmenu (m, receptionMenu, _('Commands'), '/manager/')
#		appendSubmenu (m, receptionMenu, _('External commands'), '/manager/')
#	if admin:
#		appendMenu (m, _('Administration'), '#')
#		appendSubmenu (m, _('Administration'), _('Users'), '/manager/')
#		appendSubmenu (m, _('Administration'), _('Channels'), '/manager/channel/list/')
#		appendSubmenu (m, _('Administration'), _('LOGS'), '/manager/log/')
#		appendSubmenu (m, _('Administration'), _('Queces'), '/manager/log/')
#
#		appendMenu (m, _('Stats'), '#')
#		appendSubmenu (m, _('Stats'), _('Subscriptions per channel'), '/manager/')
#		appendSubmenu (m, _('Stats'), _('Spends per channel'), '/manager/')
#		appendSubmenu (m, _('Stats'), _('Billing'), '/manager/')
#		appendSubmenu (m, _('Stats'), _('Sent chart'), '/manager/')

	appendMenu (m, _('about'), '/')

	profile['headerMenu'] = m
	return profile

class LoginForm (forms.Form):
	username = forms.CharField(max_length = 12)
	password = forms.CharField(max_length = 12, widget = forms.PasswordInput)

def index(request):
	"""
		This will be the login form.
	"""
	if not request.user.is_authenticated() :
		form = LoginForm ()
		if request.method == 'POST':
			form = LoginForm (request.POST)
			if form.is_valid ():
				user = authenticate (username = form.data['username'], password = form.data['password'])
				if user is not None:
					login (request, user)
					#info (request, 'index', _('User %s loged in ' % user.username))
					request.session['profile'] = getProfile (user)
					if request.GET.has_key ('next'):
						return HttpResponseRedirect(request.GET['next'])
					return render_to_response("index.html", {'profile' : request.session['profile']})
				else:
					#warning (request, 'index', _('Invalid username or password'))
					form.errors['username'] = [_('Invalid username or password')]
			else:
				#warning (request, 'index', _('Posted values not valid'))
				pass
		else:
			#info (request, 'index', _('Showing login form'))
			pass
	
		return render_to_response("login.html", {
			'form' : form,
			'profile' : getProfile (request.user),
			})
	else:
		request.session['profile'] = getProfile(request.user)
		return render_to_response("index.html", {'profile' : request.session['profile']})

def logoutView(request):
	"""
		This is the logout view.
	"""
	logout(request)
	return HttpResponseRedirect("/")
